﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TouchlessLib;
using System.Drawing.Imaging;

namespace Puzzle_3D
{
    public partial class Form1 : Form
    {
        public TouchlessMgr _touch = new TouchlessMgr();
        
        public Form1()
        {
            InitializeComponent();
        }

        private void loadForm(object _sender, EventArgs _event)
        {
            foreach (Camera cam in this._touch.Cameras)
            {
                if (cam != null)
                {
                    this._touch.CurrentCamera = cam;
                    cam.OnImageCaptured += new EventHandler<CameraEventArgs>(c_OnImageCaptured);
                    break;
                }
            }
            pictureBox1.Paint += new PaintEventHandler(paintCamera);
        }

        public void paintCamera(object _sender, PaintEventArgs _event)
        {
            lock (this)
            {
                if (_b != null)
                {
                    _event.Graphics.DrawImageUnscaledAndClipped(_b, pictureBox1.ClientRectangle);
                }
            }
        }

        Bitmap _b;

        
        void c_OnImageCaptured(object sender, CameraEventArgs _event)
        {
            if (button1.Enabled)
            {
                _b = _event.Image;
                pictureBox1.Invalidate();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Bitmap image = this._touch.CurrentCamera.GetCurrentImage();
            this.pictureBox1.BackgroundImage = image;
            this.button1.Enabled = false;
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (_touch.MarkerCount != 0)
            {
                _touch.RemoveMarker(0);
            }
            Marker m = _touch.AddMarker("marker", (Bitmap)pictureBox1.BackgroundImage, new System.Drawing.Point(e.X, e.Y), 10);
            m.Highlight = true;
            m.Threshold = 10;
            button1.Enabled = true;
        }

        private void start_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
